package com.mzy.xmyx.acl.util;

import com.mzy.xmyx.model.acl.Permission;

import java.util.ArrayList;
import java.util.List;

public class PermissionHelper {


    public static List<Permission> build(List<Permission> permissionAllList) {
        ArrayList<Permission> trees = new ArrayList<>();
        for (Permission permission : permissionAllList) {
            // 判断菜单
            if (permission.getPid() == 0) {
                // 一级菜单
                permission.setLevel(1);
                trees.add(findChildren(permission, permissionAllList));
            }
        }

        return trees;
    }

    private static Permission findChildren(Permission permission, List<Permission> permissionAllList) {
        permission.setChildren(new ArrayList<>());
        for (Permission it : permissionAllList) {
            if (permission.getId().equals(it.getPid())) {
                int level = permission.getLevel() + 1;
                it.setLevel(level);
                permission.getChildren().add(findChildren(it, permissionAllList));
            }
        }
        return permission;
    }
}
